package de.backtomedieval.game;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.widget.TextView;
import de.backtomedieval.R;
import de.backtomedieval.activities.gamemenu.FightScreen;
import de.backtomedieval.main.Game;
import de.backtomedieval.scenarios.AbstractDefendYourCityScenario;
import de.backtomedieval.units.UnitGroup;
import de.backtomedieval.units.UnitSet;

public class Fight {
	public static final int NO_RESULT = 0;
	public static final int WIN = 3;
	public static final int LOOSE = 6;
	
	public static final int PLAYER_DEFENDS = 0;
	public static final int PLAYER_ATTACKS = 1;

	private int round = 0;
	private UnitGroup enemy = null;
	private UnitGroup player =null;
	private City city = null;
	private int attackOrDefend = Fight.PLAYER_DEFENDS;

	private String fightString ="";
	private int res = Fight.NO_RESULT;
	private static ArrayList<Fight> fights = new ArrayList<Fight>();
	private UnitSet enemySet = null;
	private UnitSet playerSet =null;
	
	private boolean fightOver = false;
	private boolean finalFight = false;
	
	
	
	
	public Fight(UnitSet player, UnitSet enemy, int attackOrDefend, City city) {
		this.playerSet=player;
		this.enemySet=enemy;
		this.player=player.makeUnitGroup();
		this.enemy=enemy.makeUnitGroup();
		this.attackOrDefend=attackOrDefend;
		this.city=city;
		if (attackOrDefend==Fight.PLAYER_DEFENDS) {
			if (city!=null) {
				fightString = city.getName() + " " +  Game.getString(R.string.isunderattack)+ "\n\n";
			} else {
				fightString = Game.getString(R.string.ourunitsareunderattack)+ "\n\n";
			}
		} else {
			fightString = Game.getString(R.string.theattacksbegins)+ "\n\n";
		}
	}
	
	
	
	
	
	
	
	
	
	
	
	
	public boolean isFightOver() {
		return fightOver;
	}



	public void setFightOver(boolean fightOver) {
		this.fightOver = fightOver;
	}


	public int getAttackOrDefend() {
		return attackOrDefend;
	}



	public void setAttackOrDefend(int attackOrDefend) {
		this.attackOrDefend = attackOrDefend;
	}

	
	
	public boolean isFinalFight() {
		return finalFight;
	}



	public void setFinalFight(boolean finalFight) {
		this.finalFight = finalFight;
	}



	public String getFightString() {
		return fightString;
	}
	
	
	
	public int getRes() {
		return res;
	}



	public static ArrayList<Fight> getFights() {
		return fights;
	}
	
	public static void removeFight(int id) {
		fights.remove(id);
	}

	public static void fightRounds() {
		//Log.v("Fightcount: ", ""+fights.size());
		for (int i=0; i<fights.size();i++) {
			fights.get(i).fightRound();
		}
	}
	

	

	
	public void fightRound() {
		   round++;
		   if (round <= 20 && res==Fight.NO_RESULT) {
			   Log.v("Fight ", "round " + round);
			   res = fight();
		   }
		   
		   if ((round==20 || res!=Fight.NO_RESULT) && !fightOver) {
			   fightEnds();
		   }

	}

	public void fightEnds() {
	   fightOver=true;
	   if (Game.isPauseOnEndOfFight()) Game.setGamePaused(true);
	   if (finalFight) {
		   Game.getGame().setGameActive(false);
		   if (res==Fight.WIN) {
			  Game.getGame().getScenario().winScenario();
			  Game.getGame().setGameActive(false);
			  fightString = Game.getString(R.string.scenariowon)+ "\n\n" + fightString;
		   }
	   } else {
		   if (res==Fight.LOOSE && this.attackOrDefend==Fight.PLAYER_DEFENDS && city!=null) {
			   //GAMEOVER-Screen
			   fightString = city.getName() + " " +  Game.getString(R.string.isdestroyed)+ "!\n"  + Game.getString(R.string.gameover) + "\n\n" + fightString;
			   Game.getGame().setGameActive(false);
		   } else {
			   if (city!=null) {
				   city.getDefence_wall().repair();
			   }
			   UnitSet after = player.makeUnitSet();
			   if (this.attackOrDefend==Fight.PLAYER_DEFENDS) {
				   if (city!=null) {
					   city.setUnits(after);
				   }
			   }
			   
			   enemySet.clear();
			   enemySet.plusUnitSetPercent(after,Game.getDifficulty()*20);
			 

			   
		   }
	   }
	}
	
	public int fight() {
		int res = 0;
		String strRoundInfo;
		if (attackOrDefend == Fight.PLAYER_DEFENDS) {
			res = player.defenseRound(enemy, round,city);
		} else if (attackOrDefend == Fight.PLAYER_ATTACKS) {
			res = player.attackRound(enemy, round, city);
		}
		if (res==Fight.WIN) {
			strRoundInfo = Game.getString(R.string.round) +" " + round + ":\n"+Game.getString(R.string.win) +"\n" + player.getUnits().size() + " " + Game.getString(R.string.unitssurvived) + ".\n(" + player.makeUnitSet()+ ")";
		} else if (res==Fight.LOOSE){
			strRoundInfo = Game.getString(R.string.round) +" " + round + ":\n"+Game.getString(R.string.lose)+"\n" + enemy.getUnits().size() + " " +Game.getString(R.string.enemyunitssurvived)+".\n(" + enemy.makeUnitSet() + ")";
		} else {
			strRoundInfo = Game.getString(R.string.round) +" " + round + ":\n"+ Game.getString(R.string.player)+ " " + player.getUnits().size() + " " + Game.getString(R.string.units) + " | " + Game.getString(R.string.enemy) + " " + enemy.getUnits().size() + " " + Game.getString(R.string.units) +
			".\n";
			if (city!=null && city.getDefence_wall()!=null && city.getDefence_wall().getHealthpoints()!=0) {
				strRoundInfo = strRoundInfo + city.getDefence_wall().getName() +  ": " + city.getDefence_wall().getHealthpoints() +" "+  Game.getString(R.string.healthpoints) + ".\n" ; 
			}
			strRoundInfo = strRoundInfo + Game.getString(R.string.player) +": " + player.makeUnitSet() + "\n"+ Game.getString(R.string.enemy) +": " + enemy.makeUnitSet();
		}


		
		
		fightString = strRoundInfo + "\n\n"+ fightString ;
		return res;
	}



	public static void addFight(Fight fight) {
		fights.add(fight);
	}
	
	
	public void start() {
		if (Game.isPauseOnStartOfFight()) {
			Game.setGamePaused(true);
		}
		Fight.addFight(this);
		Activity last = Game.getLastActivity();
		Intent viewUnitsIntent = new Intent(last,
				FightScreen.class);
		last.startActivity(viewUnitsIntent);
		last.finish();
	}
	

	
	
}
